package com.fingard.dsp.bank.directbank.smbc01;

import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.CSVUtils;
import com.fingard.util.DateHelper;
import com.fingard.util.ForeignBankUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SMBC01SinglePay extends SMBC01Base implements ISinglePay {

    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        //拼接报文
        StringBuilder context = PaymentXMLToFile(p_req1908);
        //生成付款文件目录
        String payDate=DateHelper.convertD10toD8(p_req1908.singleDetail.payDate);
        String payFilePath = getBankConfig().getValueAsString("PayFilePath")+ File.separator +payDate +File.separator
                +"GCMS"+payDate.substring(2,8)+"_"+"DFT"+"_RMB_"+p_req1908.singleDetail.askNumber+".csv";
        WriteBankLogLn("请求银行的报文："+context.toString());
        foreignBankHelper=new ForeignBankUtil(getBankConfig(),bankFront,getSbLog(),getCharset());
        //生成付款文件并上传
        String[] bankResult=new String[]{"",""};
        //付款文件不需要加密
        List<String> list = new ArrayList<String>();
        list.add(context.toString());
        if(CSVUtils.importCsvIsUf8(payFilePath, list,getCharset())){
            //上传文件到ftp/sftp
            if(foreignBankHelper.uploadFile(payFilePath,"sftpserver")){
                bankResult[0]="ACCP";
                bankResult[1]="上传成功";
            }else {
                bankResult[0]="ERRO";
                bankResult[1]="上传失败";
            }
        }else{
            bankResult[0]="ERRO";
            bankResult[1]="写入文件失败";
        }
        //响应到ats
        detail.respCode = "0";
        detail.respInfo = bankResult[1];
        detail.transState = bankResult[0];
        detail.payInfoCode = detail.transState;
        detail.reqSeqID=p_req1908.singleDetail.askNumber;
        retAts.writeRetDetail(detail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = null;
        //处理ack文件或者MT文件，获取支付状态  result={"付款状态码","响应信息"}
        foreignBankHelper=new ForeignBankUtil(getBankConfig(),bankFront,getSbLog(),getCharset());
        String[] result=foreignBankHelper.getPayStatus(p_req1944.askNumber,p_req1944.getBgnDateTime());
        if(result.length==2 && StringHelper.hasAnyChar(result[0])){
            retDetail = new RetAtsSglDetail();
            retDetail.respCode = "0";
            retDetail.respInfo=result[1];
            retDetail.transState = result[0];
            retDetail.payInfoCode = retDetail.transState;
            retDetail.reqSeqID=p_req1944.askNumber;
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
